/*
* 定义路由表*/

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

// 基本路由表：所有角色都能够访问该路由下的信息
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/logout',
    component: () => import('@/views/login/logout'),
    hidden: true
  },
  {
    path: '/login-fail',
    component: () => import('@/views/login/iam-fail'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/error',
    component: Layout,
    hidden: true,
    redirect: 'noRedirect',
    children: [
      {
        path: '401',
        component: () => import('@/views/error-page/401'),
        hidden: true
      },
      {
        path: '404',
        component: () => import('@/views/error-page/404'),
        hidden: true
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/portal',
    children: [
      {
        path: '/portal',
        component: () => import('@/views/portal/index'),
        name: 'Portal',
        meta: { title: '首页', icon: 'el-icon-s-home', affix: true }
      }
    ]
  },
  {
    path: '/iframes',
    component: Layout,
    redirect: 'noRedirect',
    meta: {
      title: '',
      icon: 'link'
    },
    children: [
      {
        path: 'menu/:id(\\d+)',
        component: () => import('@/views/iframes/menu_iframe'),
        name: 'MenuIFrame',
        meta: { title: '', icon: 'guide', noCache: false },
        hidden: true
      }
    ]
  }
]

import Iam from './modules/iam'
import Sys from './modules/sys'

// 异步路由表：只有拥有权限（对应角色）的用户，才能显示的路由表
export const asyncRoutes = [
  Iam,
  Sys,
  { path: '*', redirect: '/error/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes.concat(asyncRoutes)
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
